﻿namespace UnityEditor
{
    using System;
    using UnityEngine;

    internal class TerrainMenus
    {
        [UnityEditor.MenuItem("Terrain/Create Terrain")]
        private static void CreateTerrain()
        {
            if (EditorUserBuildSettings.selectedBuildTargetGroup == BuildTargetGroup.Wii)
            {
                Debug.LogError("Terrain is not supported by selected platform.");
            }
            else
            {
                TerrainData asset = new TerrainData {
                    heightmapResolution = 0x201,
                    size = new Vector3(2000f, 600f, 2000f),
                    heightmapResolution = 0x200,
                    baseMapResolution = 0x400
                };
                asset.SetDetailResolution(0x400, asset.detailResolutionPerPatch);
                AssetDatabase.CreateAsset(asset, AssetDatabase.GenerateUniqueAssetPath("Assets/New Terrain.asset"));
                Selection.activeObject = Terrain.CreateTerrainGameObject(asset);
            }
        }

        [UnityEditor.MenuItem("Terrain/Export Heightmap - Raw...")]
        private static void ExportHeightmapRaw()
        {
            ScriptableWizard.DisplayWizard<ExportRawHeightmap>("Export Heightmap", "Export").InitializeDefaults(GetActiveTerrain());
        }

        [UnityEditor.MenuItem("Terrain/Flatten Heightmap...")]
        private static void Flatten()
        {
            ScriptableWizard.DisplayWizard<FlattenHeightmap>("Flatten Heightmap", "Flatten").InitializeDefaults(GetActiveTerrain());
        }

        private static void FlushHeightmapModification()
        {
            GetActiveTerrain().Flush();
        }

        private static Terrain GetActiveTerrain()
        {
            UnityEngine.Object[] filtered = Selection.GetFiltered(typeof(Terrain), UnityEditor.SelectionMode.Editable);
            if (filtered.Length != 0)
            {
                return (filtered[0] as Terrain);
            }
            return Terrain.activeTerrain;
        }

        private static TerrainData GetActiveTerrainData()
        {
            if (GetActiveTerrain() != null)
            {
                return GetActiveTerrain().terrainData;
            }
            return null;
        }

        [UnityEditor.MenuItem("Terrain/Import Heightmap - Raw...")]
        private static void ImportRaw()
        {
            string path = EditorUtility.OpenFilePanel("Import Raw Heightmap", string.Empty, "raw");
            if (path != string.Empty)
            {
                ScriptableWizard.DisplayWizard<ImportRawHeightmap>("Import Heightmap", "Import").InitializeImportRaw(GetActiveTerrain(), path);
            }
        }

        [UnityEditor.MenuItem("Terrain/Mass Place Trees...")]
        private static void MassPlaceTrees()
        {
            ScriptableWizard.DisplayWizard<PlaceTreeWizard>("Place Trees", "Place").InitializeDefaults(GetActiveTerrain());
        }

        [UnityEditor.MenuItem("Terrain/Refresh Tree and Detail Prototypes")]
        private static void RefreshPrototypes()
        {
            GetActiveTerrainData().RefreshPrototypes();
            GetActiveTerrain().Flush();
        }

        [UnityEditor.MenuItem("Terrain/Set Resolution...")]
        private static void SetHeightmapResolution()
        {
            ScriptableWizard.DisplayWizard<SetResolutionWizard>("Set Heightmap resolution", "Set Resolution").InitializeDefaults(GetActiveTerrain());
        }

        [UnityEditor.MenuItem("Terrain/Create Lightmap...", true), UnityEditor.MenuItem("Terrain/Mass Place Trees...", true), UnityEditor.MenuItem("Terrain/Flatten Heightmap...", true), UnityEditor.MenuItem("Terrain/Refresh Tree and Detail Prototypes", true), UnityEditor.MenuItem("Terrain/Set Resolution...", true), UnityEditor.MenuItem("Terrain/Export Heightmap - Raw...", true), UnityEditor.MenuItem("Terrain/Import Heightmap - Raw...", true)]
        private static bool Validate()
        {
            return (GetActiveTerrainData() != null);
        }
    }
}

